var pageObj = {
    init: function () {
        this.slideBox();
        //this.canEat();
        this.floorSlide();
        this.floorNav();
    },
    slideBox: function () {
        $("#J_slideBox").cxSlide({
            type: "fade",
            speed: 300,
            auto: true
        });
    },
    canEat: function () {
        //能不能吃 选择控件
        var $iptSlc = $(".ipt-slc");
        $iptSlc.on("click",".cur,.arrow-wrap", function () {
            $(this).closest("dl").toggleClass("active").siblings("dl").removeClass("active");
            return false;
        });
        $iptSlc.find(".cur").nextAll().on("click", function () {
            var $this = $(this),
                html = $this.html(),
                val = $this.attr("data-value");
            $iptSlc.closest("dl").removeClass("active");
            $this.siblings(".cur").html(html).attr("data-value", val);
            return false;
        });
        $("body").on("click", function () {
            $iptSlc.closest("dl").removeClass("active");
        });

        //能不能吃 表单提交
        $("#J_sub").on("click", function () {
            var $this = $(this),
                $form = $this.closest("form"),
                params = '',
                $cur = $form.find(".cur");
            $cur.each(function () {
                var $this = $(this),
                    cate = $this.closest("ul").attr("data-name"),
                    value = $this.attr("data-value");
                params = params + cate + value + '_';
            })
            window.open($this.attr("data-link") + params + 'p1')
            return false;
        });
    },
    floorSlide: function () {
        var $wrap = $("#J_floorSlide").find(".floor-nav");
        var timer = null;
        $wrap.on("mouseenter", ".J_areaB,.J_areaA", function () {
            var $this = $(this);
            clearTimeout(timer);
            timer = setTimeout(function () {
                var target = -$this.width() * 3;
                if($this.hasClass("J_areaA")){
                    target = 0;
                }
                $wrap.animate({"margin-left": target});
            },300);
        });
    },
    floorNav: function () {
        var $w = $(window);
        var $floor = $(".ctn-floor");
        var $nav = $("#J_sildeNav");
        var $navItem = $nav.find(".J_item");
        var $navItems = $("#J_floorSlide").find(".nav-item");
        var showOffset = $floor.eq(0).offset().top-100;
        $w.on("scroll", function () {
            var $this = $(this);
            var cur = $this.scrollTop();
            var flag = false;
            $floor.each(function (i,v) {
                var $this = $(this);
                if(onScreen($this,cur)){
                    flag = true;
                    $navItem.eq(i).addClass("active").siblings().removeClass("active");
                    $navItems.eq(i).find("a").addClass("active");
                    $navItems.eq(i).siblings().find("a").removeClass("active");
                    return false;
                }
            });
            if(!flag){
                $navItem.removeClass("active");
            }
            var cur = $w.scrollTop();
            if(cur>showOffset){
                $nav.fadeIn();
            }else{
                $nav.fadeOut();
            }
        }).trigger("scroll");
        var wH = $w.height();
        function onScreen($elm,curScrollTop){
            var elmTop = $elm.offset().top;
            var elmTopH = $elm.offset().top+$elm.height();
            return !(elmTop < curScrollTop && elmTopH<curScrollTop+100);
        }
        $navItem.on("click", clickHandler);
        $navItems.on("click", clickHandler);
        function clickHandler(){
            var $this = $(this);
            var $fl = $floor.eq($this.index());
            $("html,body").animate({
                scrollTop: $fl.offset().top
            });
        }
        $(".J_top").on("click", function () {
            $("html,body").animate({
                scrollTop: 0
            })
        });

    }
};
$(function () {
    pageObj.init();
});
